package com.jule.teenpatti.game.network.protocol.reqs;

import JoloProtobuf.GameSvr.JoloGame;
import com.jule.core.common.log.LoggerUtils;
import com.jule.teenpatti.base.enums.GameConst;
import com.jule.teenpatti.game.model.PlayerInfo;
import com.jule.teenpatti.game.network.protocol.ClientReq;
import com.jule.teenpatti.game.network.protocol.acks.JoloGame_ChatAck_80001;
import com.jule.teenpatti.game.play.AbstractTable;
import com.jule.teenpatti.game.service.TableService;
import com.jule.teenpatti.game.service.holder.FunctionIdHolder;
import com.jule.teenpatti.game.utils.log.TableLogUtil;
import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;

/**
 * 聊天
 */
@Slf4j
public class JoloGame_ChatReq_80001 extends ClientReq {
    private long time;

    public JoloGame_ChatReq_80001(int functionId) {
        super(functionId);
    }

    private JoloGame.JoloGame_ChatReq req;


    @Override
    public void readPayLoadImpl(ByteBuf buf) throws Exception {
        time = System.currentTimeMillis();
        byte[] blob = new byte[buf.readableBytes()];
        buf.readBytes(blob);
        req = JoloGame.JoloGame_ChatReq.parseFrom(blob);
        this.setTable(TableService.getInstance().getTable(header.gameId + "", req.getRoomId(), req.getTableId()));
    }

    @Override
    public void processImpl() throws Exception {
        try {
            log.debug("收到消息-> " + functionId + " reqNum-> " + header.reqNum + "msg ->" + req.toString());
            JoloGame.JoloGame_ChatAck.Builder builder = JoloGame.JoloGame_ChatAck.newBuilder();
            String userId = req.getUserId();

            AbstractTable table = getTable();
            if (table == null) {
                log.warn("找不到房间 id->" + req.getTableId());
                return;
            }
            StringBuilder sb = new StringBuilder();
            sb.append("chatType:");
            if (req.hasIcoMesg()) {//这种默认值的赋值方式需要和前端商量好
                sb.append("IconMsg,content:" + req.getIcoMesg());
                table.boardcastMessage(table.getTableId(), JoloGame.JoloGame_ChatMesgSend.newBuilder().
                        setUserId(userId).setRoomId(req.getRoomId()).setTableId(req.getTableId())
                        .setIcoMesg(req.getIcoMesg()).build(), FunctionIdHolder.Game_Notice_CHAT);
                ctx.writeAndFlush(new JoloGame_ChatAck_80001(builder.setResult(1).build(), header));
            } else if (req.hasMesg()) {
                sb.append("TextMsg,content:" + req.getMesg());
                String msg = req.getMesg();
                if (msg.length() > 30) {
                    ctx.writeAndFlush(new JoloGame_ChatAck_80001(builder.setResult(0).setResultMsg("长度超过30").build(), header));
                    return;
                }

                table.boardcastMessage(table.getTableId(), JoloGame.JoloGame_ChatMesgSend.newBuilder().
                        setUserId(userId).setRoomId(req.getRoomId()).setTableId(req.getTableId())
                        .setMesg(msg).build(), FunctionIdHolder.Game_Notice_CHAT);
                ctx.writeAndFlush(new JoloGame_ChatAck_80001(builder.setResult(1).build(), header));
            }
            String nickName = "";
            PlayerInfo playerInfo = table.getPlayer(userId);
            if (playerInfo != null) {
                nickName = playerInfo.getNickName();
            }
            TableLogUtil.chat(functionId, "chat", userId, nickName, table.getPlayType() + "",
                    table.getRoomId(), table.getTableId(), sb.toString());
            long timeMillis = System.currentTimeMillis() - time;
            if (timeMillis > GameConst.COST_TIME) {
                LoggerUtils.performance.info("ChatReq_80001,cost time:{},req:{}", timeMillis, req.toString());
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
        }

    }
}
